package com.violet.base.server.dic.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.violet.base.api.dic.condiftion.DictTypeCondition;
import com.violet.base.api.dic.dto.DicTypeDTO;
import com.violet.base.server.dic.entity.DictTypeEntity;
import com.violet.base.server.dic.mapper.DictTypeMapper;
import com.violet.base.server.dic.service.DictTypeService;
import com.violet.common.exception.BusinessException;
import com.violet.common.page.PageParam;
import com.violet.common.page.PageResult;
import com.violet.common.page.SortParam;
import com.violet.db.util.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wqf
 * @Date: 2022/10/27
 * @Description:
 */
@Service
public class DictTypeServiceImpl implements DictTypeService {

    @Resource
    private DictTypeMapper dictTypeMapper;

    /**
     * 方法描述: 分页查询字典类型
     *
     * @param condition 查询入参
     * @return com.violet.db.entity.PageResult<com.violet.base.server.dic.dto.DicTypeDTO>
     * @author wqf
     * @date 2022/10/27 16:46
     */
    @Override
    public PageResult<DicTypeDTO> listDictType(DictTypeCondition condition, PageParam pageParam, SortParam sortParam) {
        Page<DictTypeEntity> page = PageUtils.getPage(pageParam, sortParam);
        QueryWrapper<DictTypeEntity> queryWrapper = new QueryWrapper<DictTypeEntity>()
                .like(StrUtil.isNotBlank(condition.getDicTypeCode()), "dic_type_code", condition.getDicTypeCode())
                .like(StrUtil.isNotBlank(condition.getDicTypeName()), "dic_type_name", condition.getDicTypeName());
        Page<DictTypeEntity> dictTypeEntityPage = dictTypeMapper.selectPage(page, queryWrapper);
        PageResult<DicTypeDTO> pageResult = new PageResult<>();
        BeanUtils.copyProperties(dictTypeEntityPage, pageResult);
        List<DictTypeEntity> records = dictTypeEntityPage.getRecords();
        if (records.isEmpty()) {
            pageResult.setRecords(new ArrayList<>());
        }
        List<DicTypeDTO> dicTypeDTOList = new ArrayList<>(records.size());
        for (DictTypeEntity typeEntity : records) {
            DicTypeDTO dicTypeDTO = new DicTypeDTO();
            BeanUtils.copyProperties(typeEntity, dicTypeDTO);
            dicTypeDTOList.add(dicTypeDTO);
        }
        pageResult.setRecords(dicTypeDTOList);
        return pageResult;
    }

    /**
     * 方法描述: 新增或更新字典类型
     *
     * @param dicTypeDTO 新增或更新字典类型入参
     * @author wqf
     * @date 2022/10/27 11:51
     */
    @Override
    public void saveOrUpdateDictType(DicTypeDTO dicTypeDTO) {
        Long id = dicTypeDTO.getId();
        String dictTypeCode = dicTypeDTO.getDicTypeCode();
        String dictTypeName = dicTypeDTO.getDicTypeName();
        //校验字典名称是否重复
        QueryWrapper<DictTypeEntity> itemNameQueryWrap = new QueryWrapper<DictTypeEntity>()
                .eq("dict_type_name", dictTypeName);
        DictTypeEntity itemNameEntity = dictTypeMapper.selectOne(itemNameQueryWrap);
        if (itemNameEntity != null && !itemNameEntity.getId().equals(id)) {
            throw new BusinessException("字典分类名称已存在");
        }
        //校验字典值是否重复
        QueryWrapper<DictTypeEntity> itemValueQueryWrap = new QueryWrapper<DictTypeEntity>()
                .eq("dict_item_code", dictTypeCode);
        DictTypeEntity itemValueEntity = dictTypeMapper.selectOne(itemValueQueryWrap);
        if (itemValueEntity != null && !itemValueEntity.getId().equals(id)) {
            throw new BusinessException("字典分类编码已存在");
        }
        DictTypeEntity dictTypeEntity = new DictTypeEntity();
        BeanUtils.copyProperties(dicTypeDTO, dictTypeEntity);
        if (id != null) {
            dictTypeMapper.updateById(dictTypeEntity);
        } else {
            dictTypeMapper.insert(dictTypeEntity);
        }
    }

    /**
     * 方法描述: 根据主键id删除字典分类
     *
     * @param id 主键id
     * @author wqf
     * @date 2022/10/27 16:42
     */
    @Override
    public void deleteDictTypeById(Long id) {
        DictTypeEntity dictTypeEntity = dictTypeMapper.selectById(id);
        if (dictTypeEntity == null) {
            throw new BusinessException("资源不存在");
        }
        dictTypeMapper.deleteById(id);
    }
}
